﻿
    using Microsoft.Win32;
    using System;
    using System.IO;
using System.Runtime.Serialization;
using System.Security.Cryptography;
    using System.Text;

namespace DevWpfApp
{
    public class CryptoHelper
    {
        static RijndaelManaged mobjCryptoService;
        private static byte[] GetLegalKey(string key)
        {
            string s = key;
            mobjCryptoService.GenerateKey();
            int length = mobjCryptoService.Key.Length;
            if (s.Length > length)
            {
                s = s.Substring(0, length);
            }
            else if (s.Length < length)
            {
                s = s.PadRight(length, ' ');
            }
            return Encoding.ASCII.GetBytes(s);
        }
        private static byte[] GetLegalIV(string key)
        {
            string s = key;
            mobjCryptoService.GenerateIV();
            int length = mobjCryptoService.IV.Length;
            if (s.Length > length)
            {
                s = s.Substring(0, length);
            }
            else if (s.Length < length)
            {
                s = s.PadRight(length, ' ');
            }
            return Encoding.ASCII.GetBytes(s);
        }

        public static void SaveXml<T>(string filename, string pwd, T obj) where T : new()
        {
            mobjCryptoService = new RijndaelManaged();
            using (FileStream stream = File.Create(filename))
            {
                mobjCryptoService.Key = GetLegalKey(pwd);
                mobjCryptoService.IV = GetLegalIV(pwd);
                ICryptoTransform transform = mobjCryptoService.CreateEncryptor();
                using (CryptoStream output = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                {
                    DataContractSerializer s = new DataContractSerializer(typeof(T));
                    s.WriteObject(output, obj);
                }
            }
        }
        public static T LoadXml<T>(string filename, string pwd) where T : new()
        {
            mobjCryptoService = new RijndaelManaged();
            using (FileStream stream = File.Open(filename, FileMode.Open))
            {
                mobjCryptoService.Key = GetLegalKey(pwd);
                mobjCryptoService.IV = GetLegalIV(pwd);
                ICryptoTransform transform = mobjCryptoService.CreateDecryptor();
                

                DataContractSerializer s = new DataContractSerializer(typeof(T));

                using (CryptoStream input = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                {
                    T obj = new T();
                    Object S2 = s.ReadObject(input);
                    return (T)S2;
                }
            }
        }

        public static void SaveXml<T>(string filepath, T obj) where T : new()
        {
            DataContractSerializer s = new DataContractSerializer(typeof(T));
            using (FileStream fs = File.Open(filepath, FileMode.Create))
            {
                s.WriteObject(fs, obj);
            }
        }
        public static T LoadXml<T>(string filepath) where T : new()
        {
            DataContractSerializer s = new DataContractSerializer(typeof(T));

            using (FileStream fs = File.Open(filepath, FileMode.Open))
            {
                T obj = new T();
                Object S2 = s.ReadObject(fs);
                return (T)S2;
            }
        }

        public static bool Save<T>(string filename, string _password, T obj) where T : new()
        {
            if (_password != "")
            {
                SaveXml<T>(filename, _password, obj);
            }
            else
            {                
                SaveXml<T>(filename, obj);
            }
            return true;
        }
        public static T Load<T>(string filename, string password) where T : new()
        {
            if (password != "")
            {
                return LoadXml<T>(filename, password);
            }
            else
            {
                return LoadXml<T>(filename);
            }
        }

    }
}
